package com.baidu.controller;

import com.alibaba.fastjson.JSON;
import com.baidu.pojo.PowerBean;
import com.baidu.pojo.RoleBean;
import com.baidu.pojo.UserBean;
import com.baidu.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * 创作时间：2019/10/24 14:53
 * 作者：李增强
 */
@Controller
public class UserController {

    @Resource
    UserService userService;

    /**
     * 查看用户的权限
     */
    @RequestMapping("/getUserPower")
    public String getUserPower(Integer id,Model model){
        List<PowerBean> list = userService.getUserPowerById(id);
        String json = JSON.toJSONString(list);
        model.addAttribute("json", json);
        return "user_power";
    }

    /**
     * 保存用户分配角色
     */
    @RequestMapping("/updateUserRole")
    public String updateUserRole(Integer id,Integer rid){
        userService.updateUserRole(id,rid);
        return "redirect:getUserList.do";
    }


    /**
     * 去用户分配角色
     */
    @RequestMapping("/toUserRole")
    public String toUserRole(Integer id,Model model){
        UserBean ub = userService.getUserById(id);
        List<RoleBean> list = userService.getRoleList();
        model.addAttribute("ub", ub);
        model.addAttribute("list", list);
        return "user_role";
    }


    /**
     * 修改用户状态
     */
    @RequestMapping("/changeUserStatus")
    public String changeUserStatus(Integer id,Integer status){
        userService.updateUserStatus(id,status);
        return "redirect:getUserList.do";
    }

    @RequestMapping("/getLogin")
    public String getLogin(String uname,String pwd){
        UserBean ub = userService.getLogin(uname,pwd);
        if(ub!=null){
            return "main";
        }
        return "../../index";
    }

    @RequestMapping("/getUserList")
    public String getUserList(Model model){
        List<UserBean> list = userService.getUserList();
        model.addAttribute("list", list);
        return "user_list";
    }
}
